package IO;

import java.io.*;
import java.util.Scanner;

public class Exercise2 {
    //把一个文件拷贝到另外一个文件
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入拷贝文件地址: ");
        String readName = scanner.next();
        File fileRead = new File(readName);
        if(fileRead.isDirectory() || !fileRead.exists()) return;
        System.out.print("输入文件拷贝地址: ");
        String writeName = scanner.next();
        File fileWrite = new File(writeName);
        //如果文件存在就不能拷贝不然会把原文件内容抹除
        if(fileWrite.isDirectory() || fileWrite.exists()) return;
        try (Writer writer = new FileWriter(writeName)) {
            try (Reader reader = new FileReader(readName)) {
                while (true) {
                    int ch = reader.read();
                    if(ch == -1) break;
                    char ch1 = (char) ch;
                    writer.write("" + ch1);
                }
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
